﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using SimpleRest.Core.Routing;

namespace SimpleRest.Core
{
    /// <summary>
    /// Class that implements IHttpRequestMessage which the internal core assembly uses to model the REST request.
    /// </summary>
    public class HttpRequestMessage : IHttpRequestMessage
    {
        public HttpRequestMessage() { }
        public HttpRequestMessage(RequestContext context)
        {
            var request = context.HttpContext.Request;
            this.Content = request.InputStream;

            List<Header> headers = new List<Header>();
            if (request.Headers != null)
                foreach (var headerKey in request.Headers.AllKeys)
                    headers.Add(new Header() { Key = headerKey, Value = request.Headers[headerKey] });
            this.Headers = headers;
            this.Uri = System.Web.HttpUtility.UrlDecode(request.Url.AbsoluteUri);
            this.Verb = request.HttpMethod;
            List<Cookie> cookies = new List<Cookie>();
            if (request.Cookies != null)
                foreach (var cookieKey in request.Cookies.AllKeys)
                {
                    var c = request.Cookies[cookieKey];
                    cookies.Add(new Cookie() { Domain = c.Domain, IsHttpOnly = c.HttpOnly, IsSecure = c.Secure, Key = cookieKey, Path = c.Path, Value = c.Value });
                }
            this.Cookies = cookies;
            this.UserHostAddress = request.UserHostAddress;
            if (context.RouteData.Values != null)
            {
                this.RouteInformation = new RouteInformation(context.RouteData.Route.GetVirtualPath(context, context.RouteData.Values).VirtualPath);
                if (this.RouteInformation.RouteData == null)
                    this.RouteInformation.RouteData = new Dictionary<string, object>();
                foreach (KeyValuePair<string, object> kvp in context.RouteData.Values)
                {
                    this.RouteInformation.RouteData.Add(kvp);
                }
            }
        }

        /// <summary>
        /// Content for the associated REST request.
        /// </summary>
        public System.IO.Stream Content { get; set; }
        /// <summary>
        /// Headers present on the REST request.
        /// </summary>
        public List<Header> Headers { get; set; }
        /// <summary>
        /// Incoming URI for the REST request.
        /// </summary>
        public string Uri { get; set; }
        /// <summary>
        /// Incoming Http VERB for the REST request.
        /// </summary>
        public string Verb { get; set; }
        /// <summary>
        /// Cookies present on the REST request.
        /// </summary>
        public List<Cookie> Cookies { get; set; }
        /// <summary>
        /// The user host address, or ip address, for the REST request.
        /// </summary>
        public string UserHostAddress { get; set; }
        /// <summary>
        /// Route information associated to the request.
        /// </summary>
        public RouteInformation RouteInformation { get; set; }
    }
}
